# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: manual_testing.py
  @time: 2022/5/21 15:43
  @desc:
"""
import time
from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.application.LASER_App.N7786C_App import N7786C_App
from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver
from utils.read_ini_2_dict import get_option_value, get_refrence_data
from utils.get_wave_power_util import get_wave_power_value
from ATS.util.calcu_step_util import get_step_list
from gui import constant
from utils.save_setting_2_ini import save_ini_info


def manual_testing(die_num, subdie_num, align_wave=1310, start_wave=1260, stop_wave=1360, step_wave=1, l8164_power=0,
                   n7744_power_range=0,
                   n7744_atim="100us", max_min=1):
    l8164b_app = L8164B_App("SS3")
    n7744c_app = N7744C_App("SS2")
    n7786c_app = N7786C_App("SS1")
    l8164b_app.set_wave_length(align_wave)
    l8164b_app.set_power_unit()
    l8164b_app.set_power_value(l8164_power)
    p_in = l8164b_app.fetch_input_power()
    time.sleep(4)
    l8164b_app.output_on()
    n7744c_app.reset()
    n7744c_app.set_power_range(n7744_power_range)
    n7744c_app.set_auto_gain()
    n7744c_app.trigger_input_mode()
    n7744c_app.set_power_wave_length(align_wave)
    n7744c_app.set_logging_points()
    n7786c_app.reset()
    n7786c_app.set_stabilization_mode(0)
    n7786c_app.set_random_points_stepwidth()
    n7786c_app.set_trigger_conf_mode(4)
    n7786c_app.set_sample_point_rate()
    n7786c_app.set_wave_length(align_wave)
    n7786c_app.start_polarimeter_logging()
    n7786c_app.start_polarization_sequence()
    n7744c_app.set_logging_state("START")
    n7786c_app.trigger()
    n7744c_app.tigger_status()
    while True:
        state = n7744c_app.get_func_state()
        if state == "LOGGING_STABILITY,COMPLETE":
            result_list = n7744c_app.get_func_res()
            break
    max_index = result_list.index(max(result_list))
    min_index = result_list.index(min(result_list))
    while True:
        log_value = n7786c_app.get_logged_value()
        if log_value == "+0":
            polarization_data = n7786c_app.get_polarization_sweep_data()
            break
    n7786c_app.stop_logging_polcontroller()
    state = n7786c_app.get_opc_state()
    if state == "1":
        n7786c_app.set_external_trigger_delay()
        n7786c_app.set_trigger_conf_mode()
        n7786c_app.set_stabilization_mode(1)

    max_polar = [polarization_data[max_index * 3], polarization_data[max_index * 3 + 1],
                 polarization_data[max_index * 3 + 2]]
    min_polar = [polarization_data[min_index * 3], polarization_data[min_index * 3 + 1],
                 polarization_data[min_index * 3 + 2]]
    if max_min:
        n7786c_app.set_polarization_value(*max_polar)
    else:
        n7786c_app.set_polarization_value(*min_polar)
    x_data = get_step_list(start_wave, stop_wave, step_wave)
    points = len(x_data)
    n7744c_app.set_logging_state("STOP")

    l8164b_app.set_sweep_mode()
    l8164b_app.query_sweep_pmax(start_wave, stop_wave)
    l8164b_app.set_repeat_mode()
    l8164b_app.set_wave_sweep_speed()
    l8164b_app.set_start_stop_step_wave(start_wave, stop_wave, step_wave)
    l8164b_app.trigger_config()
    l8164b_app.trigger_output()
    l8164b_app.trigger_input()
    l8164b_app.output_on()
    l8164b_app.set_wave_sweep_state()

    n7744c_app.set_power_unit()
    n7744c_app.set_power_range(n7744_power_range)
    n7744c_app.set_auto_gain()
    n7744c_app.set_power_atim(n7744_atim)
    n7744c_app.trigger_input_mode()
    n7744c_app.set_power_wave_length()
    n7744c_app.set_logging_points(points)
    n7744c_app.set_logging_state()
    n7744c_app.tigger_status()
    while True:
        state = n7744c_app.get_func_state()
        if state == "LOGGING_STABILITY,COMPLETE":
            y_data = n7744c_app.get_func_res()
            break
    y_data = [float(i) for i in y_data]
    l8164b_app.stop_sweep()

    wave_power_tuple = get_refrence_data()
    for wave_index in range(len(x_data)):
        y_data[wave_index] = float(p_in) - y_data[wave_index] - get_wave_power_value(x_data[wave_index],
                                                                                     wave_power_tuple)
    loss_gc_list = [float(i) / 2 for i in y_data]
    save_ini_info(constant.REFRENCE_FILE, f"{die_num}_{subdie_num}_loss_gc_data", "wave_data", str(x_data))
    save_ini_info(constant.REFRENCE_FILE, f"{die_num}_{subdie_num}_loss_gc_data", "loss_gc", str(loss_gc_list))


# manual_testing(3, 1)
